package com.analysis.yejin.popup.actions;

//import org.eclipse.core.internal.runtime.Activator;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
//import org.eclipse.jface.viewers.ISelectionProvider;
//import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
//import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
//import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
//import org.eclipse.ui.IWorkbenchSite;
//import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
//import org.eclipse.jdt.internal.ui.callhierarchy.OpenCallHierarchyAction;
//import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import com.analysis.yejin.data.CodeDataModel;
//import com.analysis.yejin.data.CodeHierarchy;
import com.analysis.yejin.editor.AnalysisEditor;
import com.analysis.yejin.editor.AnalysisInput;


public class AnalysisSubmenu implements IObjectActionDelegate {

	private Shell shell;
	
	/**
	 * Constructor for Action1.
	 */
	public AnalysisSubmenu() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		
		/*
		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite().getSelectionProvider().getSelection();
		IWorkbenchSite site = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
		
		System.out.println("test");
		if(selection != null && selection instanceof IStructuredSelection){
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			if(obj != null){
				OpenCallHierarchyAction callHierachy = new OpenCallHierarchyAction(site);
				callHierachy.run();
				String callId = callHierachy.getId();
				System.out.println(callId);
				System.out.println(callHierachy.getDescription());
				MessageDialog.openInformation(
	    				shell,
	    				"Yejin",
	    				callHierachy.getDescription());
			}
		}
		*/
		
		
		//new editor open
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		
		AnalysisInput input = new AnalysisInput(CodeDataModel.getInstance());
		try{
    		page.openEditor(input, AnalysisEditor.ID);
    	}catch (PartInitException e){
    		MessageDialog.openInformation(
    				shell,
    				"Yejin",
    				"New Action was executed."+e);
    		throw new RuntimeException(e);
    		
    	}
		
		
		
		/*
		// get the view
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		View view = (View) page.findView(View.ID);
		// get the selection
	    ISelection selection = view.getSite().getSelectionProvider().getSelection();

	    if (selection != null && selection instanceof IStructuredSelection) {
	    	Object obj = ((IStructuredSelection) selection).getFirstElement();
	    	// if we had a selection lets open the editor
	    	if (obj != null) {
	    		CodeHierarchy co = (CodeHierarchy) obj;
		    	AnalysisInput input = new AnalysisInput(co.getId());
		    	try{
		    		page.openEditor(input, AnalysisEditor.ID);-
		    		

		    	}catch (PartInitException e){
		    		MessageDialog.openInformation(
		    				shell,
		    				"Yejin",
		    				"New Action was executed."+e);
		    		throw new RuntimeException(e);
		    		
		    	}
	    	}
	    }
	    */
	    
		
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
